<?php
/**
 * 操作日志
 *
 * @author 孙晓晔
 * @version $Id: Network.class.php 866 2010-01-13 08:58:55Z sunxy $
 */

require_once(dirname(__FILE__) . '/../AdminUtil.class.php');

class Lock extends AdminUtil {
	public $tbl = Constant::tbl_lock;

	public $lock = array(
        '-' => '请选择',
        '1' => '采购单',
	);

	public $orderby = array(
        '-' => '请选择',
        'lock' => '业务',
		'emp' => '用户',
	
        '1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:29:07 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);

		$this->dsn = 'admin';
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('uri_cc', Common::cc());

		$controller->assign_by_ref('lock_option', $this->lock);
	}

	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function layout($controller, $request) {

		$controller->display($request, 'log/action');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
			
		$controller->assign('t1', strtotime('-1 day'));
		$controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/lock/find');
		return true;
	}
	
	function orderby($px, $orderby) {
		$px = $this->orderby_bb($px, $orderby, 'bytes DESC');

		$ret = ' order by ';

		return $ret . $this->orderby_dd($px);
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);
		
		$lock_s = $request['lock_s'];
		$lock_id_s = $request['lock_id_s'];		

		$db = newdb($this->dsn_r);
		$sql = "SELECT * FROM " . $this->tbl . " a ";
		$where = " WHERE 1=1";

		if (!empty($lock_s) && $lock_s <> '-') {
			$where .= " AND lock = '" . $lock_s . "'";
		}

		if (!empty($lock_id_s)) {
			$where .= " AND lock_id = '" . $lock_id_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		$orderby_s = $request['orderby_s'];
		$orderby = $this->orderby($orderby_s, $this->orderby);
		$sql .= $where . $orderby;
		// debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['lock_t'] = $this->lock[$row['lock']];

				if (!empty($row['emp'])) {
					$row['emp_t'] = Name::emp($row['emp']);
				}

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'log/lock/list');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}			

			$fmt = "delete from %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function t($controller, $request) {

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
}
?>
